<template>
  <el-dialog
    :title="titleMap[mode]"
    :visible.sync="visible"
    width="400px"
    destroy-on-close
    :close-on-click-modal="false"
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="所属字典" prop="cate_id">
        <el-cascader
          v-model="form.cate_id"
          :options="dic"
          :props="dicProps"
          :show-all-levels="false"
          clearable
          style="width: 100%"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="项名称" prop="key">
        <el-input v-model="form.key" clearable></el-input>
      </el-form-item>
      <el-form-item label="键值" prop="key">
        <el-input v-model="form.value" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否有效" prop="status">
        <el-switch
          v-model="form.status"
          active-value="1"
          inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false" size="small">取 消</el-button>
      <el-button
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        size="small"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ["success", "closed"],
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增项",
        edit: "编辑项",
      },
      visible: false,
      isSaveing: false,
      form: {
        id: "",
        cate_id: "",
        key: "",
        value: "",
        status: "1",
      },
      rules: {
        cate_id: [{ required: true, message: "请选择所属字典" }],
        key: [{ required: true, message: "请输入项名称" }],
        value: [{ required: true, message: "请输入键值" }],
      },
      dic: [],
      dicProps: {
        value: "id",
        label: "name",
        emitPath: false,
        checkStrictly: true,
      },
    };
  },
  mounted() {
    this.getDic();
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //获取字典列表
    async getDic() {
      var res = await this.$api.dict.tree.get();
      this.dic = res.data;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;

          let res;
          if (this.mode == "add") {
            res = await this.$api.dict.addDict.post(this.form);
          } else {
            res = await this.$api.dict.editDict.post(this.form);
          }

          this.isSaveing = false;
          if (res.code == 0) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          } else {
            this.$alert(res.msg, "提示", { type: "error" });
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      this.form.id = data.id;
      this.form.value = data.value;
      this.form.key = data.key;
      this.form.status = String(data.status);
      this.form.cate_id = data.cate_id;

      console.log(this.form);
    },
  },
};
</script>

<style>
</style>
